package com.siwei.mes.service.laboratory.impl;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.entity.laboratory.CustomData;
import com.siwei.mes.entity.sync.erp.ErpCustomer;
import com.siwei.mes.enums.SystemParamEnum;
import com.siwei.mes.mapper.laboratory.CustomInfoMapper;
import com.siwei.mes.mapper.laboratory.KeyPropertyMapper;
import com.siwei.mes.mapper.syncErp.ErpCustomerMapper;
import com.siwei.mes.service.laboratory.CustomInfoService;
import com.siwei.mes.service.system.SystemParamService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 协会客户（CUstom_Info 表）
 *
 * @Description: 协会客户（CUstom_Info 表）
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/7/26 09:30
 */
@Service
public class CustomInfoServiceImpl implements CustomInfoService {
    private static final Logger log = LoggerFactory.getLogger("laboratory");

    @Resource
    private CustomInfoMapper customInfoMapper;
    @Resource
    private KeyPropertyMapper keyPropertyMapper;
    @Resource
    private ErpCustomerMapper erpCustomerMapper;
    @Resource
    private SystemParamService systemParamService;

    @Override
    public String saveOrUpdate(ErpCustomer erpCustomer, Boolean isNotify, Boolean isMock) {
        log.info("正在处理【协会软件】新增或修改【协会客户】信息入参：{}", JSON.toJSONString(erpCustomer));

        /**判断是否开启推送混凝土任务单到协会开关*/
        String systemParamValue = systemParamService.selectBySystemCode(SystemParamEnum.PUSH_PRODUCE_HNT, "0");
        /**如果这里是模拟同步 不管是什么直接默认1*/
        if (isMock) {
            systemParamValue = "1";
        }
        if (StringUtils.isBlank(systemParamValue) || "0".equals(systemParamValue)) {
            log.info("系统参数：【{}】暂未开启推送混凝土任务单到协会软件，不推送协会客户信息！", SystemParamEnum.PUSH_PRODUCE_HNT);
            return null;
        }

        if (erpCustomer == null) {
            return null;
        }
        CustomData customData = null;
        try {
            customData = customInfoMapper.selectByCustomerName(erpCustomer.getCustomerName());
            if (customData == null) {
                customData = CustomData.payloadInfo(erpCustomer, null);
                String newId = keyPropertyMapper.getNewId();
                customData.setCustId(newId);
                customInfoMapper.insert(customData);
            } else {
                String custId = customData.getCustId();
                customData = CustomData.payloadInfo(erpCustomer, custId);
                customInfoMapper.updateByPrimaryKey(customData);
            }
            erpCustomerMapper.updateIsUploadInfo(erpCustomer.getId(), customData.getCustId(), 1, null);
            log.info("处理【协会软件】新增或修改【协会客户】成功！");
            return customData.getCustId();
        } catch (Exception e) {
            log.info("新增或修改协会客户信息失败，入参：{}", JSON.toJSONString(customData));
            log.info("新增或修改协会客户信息失败，错误信息：{}", e.getMessage(), e);
            erpCustomerMapper.updateIsUploadInfo(erpCustomer.getId(), null, 0, e.getMessage());
            assertTrue(isNotify, "上传客户失败，失败原因：【" + e.getMessage() + "】");
            return null;
        }
    }
}
